/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.xxl.adapter.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.xxl.adapter.core.XxlConvert;
import com.xxl.adapter.entity.JobLogReport;
import com.xxl.adapter.repository.JobLogReportRepository;
import com.xxl.job.admin.core.model.XxlJobLogReport;
import com.xxl.job.admin.dao.XxlJobLogReportDao;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Component;

/**
 * XxlJobLogReportDaoImpl
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Slf4j
@Component
public class JobLogReportDaoImpl implements XxlJobLogReportDao, XxlConvert {

  @Autowired private JobLogReportRepository repository;

  @Override
  public int save(XxlJobLogReport xxlJobLogReport) {
    JobLogReport jobLogReport = repository.save(this.convert(JobLogReport.class, xxlJobLogReport));
    xxlJobLogReport.setId(jobLogReport.getId());
    return jobLogReport.getId();
  }

  @Override
  public int update(XxlJobLogReport xxlJobLogReport) {
    List<JobLogReport> list =
        repository.findAll(
            ((root, query, cb) ->
                cb.equal(root.get("triggerDay"), xxlJobLogReport.getTriggerDay())));
    if (CollectionUtil.isNotEmpty(list)) {
      list.forEach(
          i -> {
            i.setRunningCount(xxlJobLogReport.getRunningCount());
            i.setSucCount(xxlJobLogReport.getSucCount());
            i.setFailCount(xxlJobLogReport.getFailCount());
          });

      repository.saveAll(list);

      return list.get(0).getId();
    }

    return xxlJobLogReport.getId();
  }

  @Override
  public List<XxlJobLogReport> queryLogReport(Date triggerDayFrom, Date triggerDayTo) {
    return this.convert(
        XxlJobLogReport.class,
        repository.findAll(
            (root, query, cb) ->
                cb.between(root.get("triggerDay"), triggerDayFrom, triggerDayTo),
            Sort.by(Order.asc("triggerDay"))));
  }

  @Override
  public XxlJobLogReport queryLogReportTotal() {
    return this.convert(XxlJobLogReport.class, repository.queryLogReportTotal());
  }

  @Override
  public Logger getLogger() {
    return log;
  }
}
